<?php
include_once("../configure/admin.config.inc.php");	//--> admin global var
include_once("db.inc.php");							//--> db global var
include_once("Smarty.class.php");					//--> out template
include_once("Operation.php");						//--> Operation
include_once("Product.php");						//--> Merchant
include_once("TaxClass.php");						//-->
include_once("Attributes.php");						//--> Attributes
include_once("TaxRates.php");						//--> 
include_once("UploadFiles.php");					//--> UploadFiles
include_once("Files.php");							//--> Files
include_once("InfoClass.php");						//--> info class
require_once("controlHeader.php");					//--> system control header
require_once("../tools/fckeditor/fckeditor.php") ;

$objOperate 	= new Operation($objSession->getLanguage());		//--> Operation
$objProduct		= new Product(DB_TAG_PUBLIC, $uid);					//--> product
$objTaxClass	= new TaxClass(DB_TAG_PUBLIC, $uid);				//--> TaxClass
$objTaxRates	= new TaxRates(DB_TAG_PUBLIC, $uid);				//--> TaxRates
$objAttributes	= new Attributes(DB_TAG_PUBLIC, $uid);			//--> Attributes
$objUploadFiles	= new UploadFiles(DB_TAG_PUBLIC, $uid);				//--> UploadFiles
$objFiles		= new Files(DB_TAG_PUBLIC, $uid);					//--> Files
$objInfoClass 	= new InfoClass(DB_TAG_SYSTEM, $uid);				//--> Info Class

$error_message 	= '';

$name 			= '';
$URL 			= '';
$price			= 0;
$weight			= 0;
$model			= '';
$manufacturerID	= 0;
$taxClassID		= 0;
$UPC			= '';
$description	= '';
$taxRateID		= 0;
$strStatus		= 'normal';
$totalCount		= 0;

$shippingTypeID = 0;

$arrOperate = $objOperate->arrGetFromGroupIDAndMenuID($gid, $menuid);


$prodAttributes	= array();

if(!empty($id)){
	$arrData 		= $objProduct->getFromID($arrOperate, $id, $LANGEUAGE_ID);
	$strOldName 	= $arrData['name'];
 	
 	$URL 			= $arrData['url'];
 	$description	= $arrData['description'];
	$price			= $arrData['price'] / 100;
	$weight			= $arrData['weight'] / 100;
	$model			= $arrData['model'];
	$manufacturerID	= $arrData['manufacturerID'];
	$taxClassID		= $arrData['taxClassID'];
	$UPC			= $arrData['UPC'];
	$taxRateID		= $arrData['taxRateID'];
	$strStatus 		= $arrData['status'];
	$totalCount 	= $arrData['r_totalCount'];
 	
 	$categoryID 	= $arrData['categoryID'];
 	$shippingTypeID	= $arrData['shippingTypeID'];
 	
 	$name			= $strOldName;
 	
 	$prodAttributes = $objProduct->getHash(DB_TAG_PUBLIC, "SELECT attributeID, attributePrice FROM products_attributes WHERE productID = $id");
}

if(isset($_POST['Submit']) && $_POST['Submit'] == 'Submit'){
	$id 			= $_POST['id'];
	$name 			= $_POST['name'];
	$URL 			= $_POST['url'];
 	$description	= $_POST['description'];
	$price			= $_POST['price'];
	$weight			= $_POST['weight'];
	$model			= $_POST['model'];
	$manufacturerID	= $_POST['manufacturerID'];
	$taxClassID		= $_POST['taxClassID'];
	$UPC			= $_POST['UPC'];
	$taxRateID		= $_POST['taxRateID'];
	$strStatus 		= $_POST['status'];
	$totalCount 	= $_POST['totalCount'];
	
	$categoryID 	= $_POST['categoryID'];
	
	$attributes		= $_POST['attributes'];
	$shippingTypeID	= $_POST['shippingTypeID'];
	
	
	if(empty($error_message)){
		if(empty($name)){
			$error_message = 'The product name should\'t be empty.';
		}else if($strOldName != $name && $objProduct->IsExistName($LANGEUAGE_ID, $name)){
			$error_message = 'The product name be existed.';
		}else{
			$isReturnOrg = $objProduct->edit($arrOperate, $id, $categoryID, $price, $weight, $totalCount, $model, $manufacturerID, $taxRateID, $UPC, $URL, $LANGEUAGE_ID, $name, $description, $strStatus, $shippingTypeID);

			if($isReturnOrg){
				if(is_array($attributes)){
					$objProduct->deleteAttribute($arrOperate, $id);
					
					$prodAttributes = array();
					
					foreach($attributes as $attributeid){
						$attributePrice	 = $_POST["attribute_price_$attributeid"];
						if(!empty($attributePrice) && is_numeric($attributePrice))
							$attributePrice = $attributePrice * 100;
						else
							$attributePrice = 0;
						
						$prodAttributes[$attributeid] = $attributePrice;
						$isReturnOrg = $objProduct->addAttribute($arrOperate, $id, $attributeid, $attributePrice);
					}
				}
				
				$error_message = 'edit successfully.';
				
			}else{
				$error_message = 'edit failure.';
			}
		}
	}
}

/*----- load attributes ------*/
$arrAttributes 	= array();

$arrAllAttri 		= $objAttributes->listFromCustom($arrOperate, " WHERE languageID = $LANGEUAGE_ID AND parentid = 0 ORDER BY sorts");
foreach($arrAllAttri as $attribute){
	$attributesTmp = array();
	$values 	= $objAttributes->getAllData(DB_TAG_PUBLIC, "SELECT id, name FROM attributes WHERE languageID = $LANGEUAGE_ID AND parentid = " . $attribute['id']);
	
	foreach($values as $leafAttri){
		$attriOptions	= $objAttributes->getAllData(DB_TAG_PUBLIC, "SELECT id, keyes, valuess FROM attributes_options WHERE languageID = $LANGEUAGE_ID AND attributeID = " . $leafAttri['id']);
		
		$attriOptionstmp = array();
		foreach($attriOptions as $attriOption){
			if($attriOption['keyes'] == 'Price'){
				if(array_key_exists($leafAttri['id'], $prodAttributes)){
					$attriOption['valuess'] = $prodAttributes[$leafAttri['id']];
				}
			}
			
			$attriOptionstmp[] = $attriOption;
		}
		
		if(array_key_exists($leafAttri['id'], $prodAttributes)){
			$leafAttri['price'] 	= $prodAttributes[$leafAttri['id']];
			$leafAttri['checked'] 	= ' checked';
		}else{
			$leafAttri['price'] 	= 0;
			$leafAttri['checked'] 	= '';
		}
		
		$leafAttri['options'] = $attriOptionstmp;
		
		$attributesTmp[] = $leafAttri;
	}
	
	$attribute['attribute'] = $attributesTmp;

	$arrAttributes[] = $attribute;
}

$arrTax = array();
$arrTaxClass = $objTaxClass->getHash(DB_TAG_PUBLIC, "SELECT id, name FROM tax_class");
foreach($arrTaxClass as $key => $value){
	$where = " WHERE classid = $key";
	$arrTaxRate = $objTaxRates->listCustom($arrOperate, $where);
	foreach($arrTaxRate as $taxRates){
		$arrTax[$taxRates['id']] = $value . ' ---> ' . $taxRates['summary'];// . ' (' . $arrTaxRate['rate']/100 . ')';
	}
}

$arrManufacturer = array();
$arrManufacturer = $objTaxClass->getHash(DB_TAG_PUBLIC, "SELECT id, name FROM manufacturers WHERE languageID = $LANGEUAGE_ID");

$arrShippingType = $objTaxClass->getHash(DB_TAG_PUBLIC, "SELECT id, name FROM shipping_type WHERE languageID = $LANGEUAGE_ID");

$arrImages = $objProduct->getHash(DB_TAG_PUBLIC, "SELECT u.id, u.extName FROM upload_files u, product_images p WHERE u.id = p.imageID AND p.productID = $id AND type = " . UploadFiles::TYPE_IMAGE);


$arrStatus = array('normal' => 'Normal', 'pause' => 'pause', 'stop' => 'Stop', 'hot' => 'Hot');

/*----- load FCKEditor -----*/
$oFCKeditor = new FCKeditor('description') ;

$oFCKeditor->Height		= 300;
$oFCKeditor->BasePath	= FCKeditor_BASE_PATH;

$oFCKeditor->Value = $description;

$fckHtml = $oFCKeditor->CreateHtml() ;

/*----- load attributes ------*/
$arrSizeValue = array();
$arrSize = $objAttributes->getByName($arrOperate, $LANGEUAGE_ID, 'Size');
if(is_array($arrSize)){
	$arrSizeValue = $objAttributes->getHash(DB_TAG_PUBLIC, "SELECT id, name FROM attributes WHERE languageID = $LANGEUAGE_ID AND parentid = " . $arrSize['id']);
}

/*---------- categories -------------------*/
$arrCategories = $objInfoClass->getDir($arrOperate, $parentid = 0);


/*----- out html -----*/
$smarty = new Smarty();		//----- out template
$smarty->template_dir = TEMPLATE_SYS_DIR;
$smarty->compile_dir  = COMPILE_SYS_DIR;
$smarty->assign('menuGid', $menuGid);
$smarty->assign('menuid', $menuid);
$smarty->assign('id', $id);

$smarty->assign('error_message', $error_message);
$smarty->assign('backurl',$backurl);

$smarty->assign('arrCategories',$arrCategories);
$smarty->assign('categoryID', $categoryID);

$smarty->assign('arrImages', $arrImages);

$smarty->assign('arrTax',$arrTax);
$smarty->assign('taxRateIDS', $taxRateID);

$smarty->assign('arrStatus', $arrStatus);
$smarty->assign('StatusS', $strStatus);

$smarty->assign('arrManufacturer',$arrManufacturer);
$smarty->assign('manufacturerIDS', $manufacturerID);

$smarty->assign('name', $name);
$smarty->assign('url', $url);

$smarty->assign('price', $price);
$smarty->assign('weight', $weight);
$smarty->assign('totalCount', $totalCount);
$smarty->assign('model', $model);
$smarty->assign('UPC', $UPC);
$smarty->assign('url', $URL);

$smarty->assign('arrAttributes', $arrAttributes);

$smarty->assign('shippingTypeID', $shippingTypeID);
$smarty->assign('arrShippingType', $arrShippingType);

$smarty->assign("fckHtml", $fckHtml);

$smarty->display('listProduct_Edit.htm');
?>
